package com.blog.service;

import cn.hutool.json.JSONObject;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponse;
import com.aliyun.tea.TeaException;
import com.blog.mapper.idCardOcrMapper;
import com.blog.properties.OcrProperties;
import com.blog.seckill.business.idCardOcr;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @ClassName OcrIdCardService
 * @Destription 调用阿里云接口，实现ocr身份证识别
 * @Author 天堂小野花
 * @Date 2025/7/8  19:02
 * @Version 1.0
 */
@Service
@Slf4j
public class OcrIdCardService {

    @Autowired
    private OcrProperties ocrProperties;
    @Autowired
    idCardOcrMapper idCardOcrMapper;

    /**
     * 使用AK&SK初始化账号Client
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.ocr_api20210707.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ocr_api20210707.Client(config);
    }
    public JSONObject getJsonByOcr(String path) throws Exception{

        com.aliyun.ocr_api20210707.Client client = OcrIdCardService.createClient(ocrProperties.getAccessKeyId(),ocrProperties.getAccessKeySecret());
        com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest recognizeIdcardRequest = new com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest()
                .setUrl(path)
                .setOutputFigure(true)
                .setOutputQualityInfo(false);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            RecognizeIdcardResponse recognizeIdcardResponse = client.recognizeIdcardWithOptions(recognizeIdcardRequest, runtime);
            JSONObject jsonObject = new JSONObject(recognizeIdcardResponse.getBody().getData());
            return jsonObject;
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
           log.error(error.getMessage());
            // 诊断地址
            log.error((String) error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            log.error(error.getMessage());
            // 诊断地址
            log.error((String) error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return null;
    }

    public idCardOcr addIdCard(JSONObject dataResult) {
        JSONObject idCardJson = dataResult.getJSONObject("data").getJSONObject("face").getJSONObject("data");
        idCardOcr idocr = new idCardOcr();
        idocr.setName(idCardJson.getStr("name"));
        idocr.setAddress(idCardJson.getStr("address"));
        idocr.setEthnicity(idCardJson.getStr("ethnicity"));
        idocr.setSex(idCardJson.getStr("sex"));
        idocr.setIdNumber(idCardJson.getStr("idNumber"));
        idocr.setBirthDate(idCardJson.getStr("birthDate"));
        idCardOcrMapper.insert(idocr);
        return idocr;
    }

    public idCardOcr idCardByParentId(Integer rid) {
        return idCardOcrMapper.selectByPrimaryKey(rid);
    }
}
